package com.coolbi.onsupai.admin.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.coolbi.onsupai.core.biz.AgentBiz;
import com.coolbi.onsupai.core.entity.Agent;
import com.coolbi.security.web.action.SessionAction;

/**
 *@description:供应商负责人 Action
 *@author： cgn
 *@createdate： 2012-7-8
 */
public class AgentAction extends SessionAction {


	private static final long serialVersionUID = 1L;
	private Agent agent;
	private List<Agent>agentList;
	@Autowired
	private AgentBiz agentBiz;
	
	/**
	 * @return保存供应商负责人信息
	 * @throws Exception
	 */
	public String save() throws Exception{
		try {
			agentBiz.save(agent);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	
	/**
	 * @return更新供应商负责人信息
	 * @throws Exception
	 */
	public String update()throws Exception{
		try {
			agentBiz.update(agent);
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	/**
	 * @return删除供应商负责人信息
	 * @throws Exception
	 */
	public String delete()throws Exception{
		try {
			agentBiz.delete(agent.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	/**根据Id查看供应商负责人信息
	 * @return
	 */
	public String findById(){
		try {
			agent=agentBiz.get(agent.getId());
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}
	/**查找供应商负责人信息并分页
	 * @return
	 */
	public  String listByPage(){
		pager.setRecordCount(agentBiz.count());
		agentList=agentBiz.listByPage((pager.getCurrPageNo() - 1) * pager.getPageSize(), pager.getPageSize());
		logger.debug("agentList.size()----->"+agentList.size());
		return SUCCESS;
	}
	/**
	 * 检测手机号码是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkTelephone() {
		try {
			boolean flag = false;
			if (null == agent.getId()) {
				flag = agentBiz.checkTel(agent.getTelephone());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 检测电子邮箱是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkEmail() {
		try {
			boolean flag = false;
			if (null == agent.getId()) {
				flag = agentBiz.checkEmail(agent.getEmail());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	/**
	 * 检测负责人名称是否已被使用
	 * @return
	 * @throws Exception
	 */
	public String checkAgentName() {
		try {
			boolean flag = false;
			if (null == agent.getId()) {
				flag = agentBiz.checkAgentName(agent.getAgentName());
			}
			return ajax(String.valueOf(!flag));
		} catch (Exception e) {
			e.printStackTrace();
			return ajax("error");
		}
	}
	
	
	
	public Agent getAgent() {
		return agent;
	}
	public void setAgent(Agent agent) {
		this.agent = agent;
	}
	public List<Agent> getAgentList() {
		return agentList;
	}
	public void setAgentList(List<Agent> agentList) {
		this.agentList = agentList;
	}
	public void setAgentBiz(AgentBiz agentBiz) {
		this.agentBiz = agentBiz;
	}

}
